package edu.guet.server.controller;

import edu.guet.server.model.Course;
import edu.guet.server.model.Resource;
import edu.guet.server.model.Semester;
import edu.guet.server.model.dto.AddSemester;
import edu.guet.server.service.CourseService;
import edu.guet.server.service.ResourceService;
import edu.guet.server.service.TechSemService;
import edu.guet.server.utils.AliOSSUtils;
import edu.guet.server.utils.ResultAPI;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    ResourceService resourceService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TechSemService techSemService;

    @GetMapping("/getCourse")
    public ResultAPI getCourse(@RequestParam Integer tech_id) {
        log.info("获得教师id：" + tech_id);
        Course course = courseService.findCourseByTechId(tech_id);

        if (course != null) {
            String reUrl = resourceService.getResourceById(course.getResource_id()).getResource_url();
            course.setResource_url(reUrl);
            course.setTech_id(tech_id);
            return ResultAPI.success(course);
        } else {
            Course course1 = new Course();
            course1.setTech_id(tech_id);
            return ResultAPI.success(course1,"请创建课程！");
        }
    }

    /**
     * 添加课程
     *
     * @param course
     * @param file
     * @return
     */
    @Transactional
    @PostMapping("/addCourse")
    public ResultAPI addCourse(Course course, MultipartFile file) {
        //先处理file
        String Url = "https://re.bluepoch.com/home/img/backstory/p2.png";
//        try{
//            Url=aliOSSUtils.upload(file);
//        }catch (Exception e){
//            log.info("文件存OSS失败！");
//            return ResultAPI.error("文件上传失误，请求失败！");
//        }
        course.setResource_url(Url);
        log.info("文件路径: {}", Url);
        //添加资源获取id
        Resource resource = new Resource();
        resource.setResource_url(Url);
        resource.setResource_type("pic");
        Integer resourceId = resourceService.addResource(resource);
        //添加简介和资源id
        course.setResource_id(resourceId);
        courseService.addCourse(course);
        //添加教师团队
        techSemService.addTechCourse(course);
        log.info("添加新课程返回结果：{}", course);
        return ResultAPI.success(course);
    }

    /**
     * 修改课程基本信息
     *
     * @param course（进来的是是有semesterId的
     * @param file
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PutMapping("/updatecourse")
    ResultAPI updateSemester( Course course, MultipartFile file) {
        log.info("修改课程内容: {}", course);
        //先处理file
        String Url;
        if (file != null) {
//            try {
//                Url = aliOSSUtils.upload(file);
//            } catch (Exception e) {
//                log.info("文件存OSS失败！");
//                return ResultAPI.error("文件上传失误，请求失败！");
//            }
            Url = "https://mynoteapp-web.oss-cn-beijing.aliyuncs.com/f06cf4fc-0ff2-4e3a-b5ec-246fb6514034.png";
            //更新封面
            Integer resourceId = courseService.getCourseByCourseId(course.getCourse_id()).getResource_id();
            Resource resource = new Resource();
            resource.setResource_url(Url);
            resource.setResource_id(resourceId);
            resourceService.updateResource(resource);
            course.setResource_url(Url);
        }
        //更新修改信息
        courseService.updateCourse(course);
        return ResultAPI.success(course, "修改成功！");
    }
}
